package days29.Title20;

import java.util.Stack;

/**
 * @Author fengzihao
 * @Date 2024-05-13 20:51
 */
public class Title20 {
}
class Solution{
    public boolean isValid(String s) {
        if (s.length()%2!=0)
            return false;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{')
                stack.push(s.charAt(i));
            else {
                Character right = s.charAt(i);
                if(stack.empty())
                    return false;
                Character left = stack.pop();
                if (right==')'&&left!='(')
                    return false;
                if (right==']'&&left!='[')
                    return false;
                if (right=='}'&&left!='{')
                    return false;
            }
        }
        if (stack.empty())
            return true;
        return false;
    }
}
